package com.jzr.open.auth;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Shuangyao
 * 16:34 2018/9/9
 */
@Service("authenticationEntryPointImpl")
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response, AuthenticationException e) throws IOException {
        //httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
        //response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setStatus((Integer)request.getAttribute("vStatus"));
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        int code = (Integer)request.getAttribute("vStatus");
        String msg = (String)request.getAttribute("vMessage");
        out.write("{\"code\":" + code + ",\"status\":" + code + ",\"msg\":\"" + msg + "\"" + ",\"message\":\"" + msg + "\"}");
        out.flush();
        out.close();
    }
}
